package com.xcity.game.peakroad.combat;

import java.util.ArrayList;
import java.util.List;

import com.xcity.game.combat.Combat;
import com.xcity.game.combat.CombatBuilder;
import com.xcity.game.combat.CombatTeam;
import com.xcity.game.combat.attr.CombatAttribute;
import com.xcity.game.combat.listener.CombatListener;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.combat.unit.impl.EmptyCombatUnit;
import com.xcity.game.formation.Formation;

public class PeakRoadCombatBuilder implements CombatBuilder {

	private PeakRoadCombatable player;
	private List<PeakRoadCombatable> employees;
	private List<CombatAttribute> playerBuffs;
	private PeakRoadCombatable bossNpc;
	private List<PeakRoadCombatable> npcs;
	private List<CombatAttribute> npcBuffs;
	private CombatListener listener;
	
	public PeakRoadCombatBuilder setPlayer(PeakRoadCombatable player) {
		this.player = player;
		return this;
	}
	
	public PeakRoadCombatBuilder addEmployee(PeakRoadCombatable employee) {
		if (this.employees == null) {
			this.employees = new ArrayList<PeakRoadCombatable>(4);
		}
		this.employees.add(employee);
		return this;
	}
	
	public PeakRoadCombatBuilder setPlayerBuffs(List<CombatAttribute> buffs) {
		this.playerBuffs = buffs;
		return this;
	}
	
	public PeakRoadCombatBuilder setBossNpc(PeakRoadCombatable bossNpc) {
		this.bossNpc = bossNpc;
		return this;
	}
	
	public PeakRoadCombatBuilder addNpc(PeakRoadCombatable npc) {
		if (this.npcs == null) {
			this.npcs = new ArrayList<PeakRoadCombatable>(4);
		}
		this.npcs.add(npc);
		return this;
	}
	
	public PeakRoadCombatBuilder setNpcBuffs(List<CombatAttribute> buffs) {
		this.npcBuffs = buffs;
		return this;
	}
	
	public PeakRoadCombatBuilder setListener(CombatListener listener) {
		this.listener = listener;
		return this;
	}
	
	@Override
	public Combat build() {
		// build the player's team
		CombatTeam playerTeam = new CombatTeam();
		// add the leader in the first position
		CombatUnit playerLeader = player != null ? new PeakRoadCombatUnit(playerTeam, player, playerBuffs) : EmptyCombatUnit.INSTANCE;
		playerTeam.add(playerLeader);
		// add the members
		for (int i = 0, size = employees != null ? employees.size() : 0; i < size; i++) {
			PeakRoadCombatable e = employees.get(i);
			playerTeam.add(e != null ? new PeakRoadCombatUnit(playerTeam, e, playerBuffs) : EmptyCombatUnit.INSTANCE);
		}
		fillEmpty(playerTeam);
		
		// build the npc's team
		CombatTeam npcTeam = new CombatTeam();
		// add the leader in the first position
		CombatUnit npcLeader = bossNpc != null ? new PeakRoadCombatUnit(npcTeam, bossNpc, npcBuffs) : EmptyCombatUnit.INSTANCE;
		npcTeam.add(npcLeader);
		// add the members
		for (int i = 0, size = npcs != null ? npcs.size() : 0; i < size; i++) {
			PeakRoadCombatable npc = npcs.get(i);
			npcTeam.add(npc != null ? new PeakRoadCombatUnit(npcTeam, npc, npcBuffs) : EmptyCombatUnit.INSTANCE);
		}
		fillEmpty(npcTeam);
		
		return new Combat(playerTeam, npcTeam, Combat.Type.PEAK_ROAD, listener);
	}
	
	// 补齐空位
	private static void fillEmpty(CombatTeam team) {
		int n = CombatTeam.DEFAULT_TEAM_CAPACITY- team.size();
		while (--n >= 0) {
			team.add(EmptyCombatUnit.INSTANCE);
		}
	}

}
